package com.echo.util;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Enumeration;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class StringEchoUtil {

	public static   String getUUIdValue(){
		
		return UUID.randomUUID().toString().replaceAll("-","");
	}
	
	/** 
	    * 利用BufferedReader实现Inputstream转换成String <功能详细描述> 
	    *  
	    * @param in 
	    * @return String 
	    */  
	   public static String Inputstr2Str_Reader(InputStream in, String encode)  {   
		   String str =null;  
		   StringBuffer sb = new StringBuffer();  
	       try  
	       {  
	           if (encode == null || encode.equals("")) {     encode = "utf-8";}  
	           BufferedReader reader = new BufferedReader(new InputStreamReader(in, encode));  
	           while ((str = reader.readLine()) != null)  
	           {    sb.append(str);   }  
	           return sb.toString();  
	       }  
	       catch (UnsupportedEncodingException e1)  
	       {   e1.printStackTrace();    }  
	       catch (IOException e)  
	       {    e.printStackTrace();  }  
	       return str;  
	   }  
	   /** 
	    * 利用ByteArrayOutputStream：Inputstream------------>String <功能详细描述> 
	    *  
	    * @param in 
	    * @return 
	    * @see [类、类#方法、类#成员] 
	    */  
	   public static String Inputstr2Str_ByteArrayOutputStream(InputStream in,String encode)  
	   {  
	       ByteArrayOutputStream out = new ByteArrayOutputStream();  
	       byte[] b = new byte[1024];  
	       int len = 0;  
	       try  
	       {  
	           if (encode == null || encode.equals(""))   {   encode = "utf-8";     }  
	           while ((len = in.read(b)) > 0)  
	           {     out.write(b, 0, len);     }  
	           return out.toString(encode);  
	       }  
	       catch (IOException e)  
	       {   e.printStackTrace();}  
	       return null;  
	   }  
	   
	   
	   
	   public  static void   getRequestParames(HttpServletRequest request,Map<String,String> map){
			Enumeration enu=request.getParameterNames();
			while(enu.hasMoreElements()){
			 String paraName=(String)enu.nextElement();
		   // 	log.debug("请求参数的名称为：{} 对应的值为：{}",paraName,request.getParameter(paraName));
			  map.put(paraName, request.getParameter(paraName));
			}
			
		
		}
	   
	   
	   /**
		  * 向客户端放会固定格式的JSON字符串
		  * @param ret
		  * @param response
		  */
		public static void  toClient(Object ret,HttpServletResponse response){
			PrintWriter out = null;
			try {
				response.setContentType("application/json; charset=utf-8");
				response.setCharacterEncoding("utf-8");
				out = response.getWriter();
				out.println(ret);
			} catch (IOException e) {
				e.printStackTrace();

			}finally{
				if(out != null){
					out.flush();
					out.close();
					out = null;
				}
			}
		}
}
